package com.pin.zheng.xingmulibrary.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class DialogUtil {
    private static DialogUtil tu;

    public static DialogUtil getInstance() {
        if (tu == null) {
            tu = new DialogUtil();
        }
        return tu;
    }

    private static Toast toast;

    public void showToast(Context context, String text) {
        if (!"".equals(text)){
            try {
                if (toast != null) {
                    toast.cancel();
                }
                if (context != null) {
                    toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void showToast1(Context context, String text) {
        try {
            if (toast != null) {
                toast.cancel();
            }
            if (context != null) {
                toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 自定义Toast时间方法
     */

    public void showMyToast(Context context, String text, final int cnt) {
        showToast1(context, text);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, cnt);
    }

    private ProgressDialog pd;

    public void showPd(Context context, String message) {
        if (pd != null) {
            pd.cancel();
        }
        pd = new ProgressDialog(context);
        pd.setTitle(message);
        pd.show();
    }

    public void cancelPd() {
        if (pd != null) {
            pd.cancel();
        }
    }

    /**
     * 取消toast的显示
     */
    public void cancelToast() {
        if (toast != null) {
            toast.cancel();
        }
    }

    /**
     * 显示一个简易的dialog
     *
     * @param activity
     * @param text
     */
    public void showSimpleDialog(Activity activity, String text,
                                 DialogInterface.OnClickListener listener) {
        new AlertDialog.Builder(activity).setTitle(text)
                .setPositiveButton("确定", listener).show();
    }

    /**
     * 显示一个简易的dialog
     *
     * @param activity
     * @param text
     */
    public void showSimpleDialog(Activity activity, String text) {
        showSimpleDialog(activity, text, null);
    }

    /**
     * 显示一个简易的dialog
     *
     * @param activity
     * @param text
     */
    public void showDialog(Activity activity, String text,
                           DialogInterface.OnClickListener listener) {
        new AlertDialog.Builder(activity).setTitle(text)
                .setPositiveButton("确定", listener)
                .setNegativeButton("取消", listener)
                .show();
    }
}
